import module namespace page="http://sendmeafilter.com/common/page" at "../common/page.xqy";

declare namespace my = 'my';

declare boundary-space preserve;
declare option xdmp:mapping "false";

declare variable $checks := ('Select');
declare variable $space :="&nbsp;&nbsp;";
declare variable $affiliates := element affiliates{/affiliate};
declare variable $affiliates2 :=
<affiliates>
{
	for $i in 1 to 22
	let $f := my:rand(('Bob','Dave','Mary','Carmine','Phil','Jill','Roberto'))	
	let $l := my:rand(('Keller','Wilson','Smith','Jones','Anthropolis','Thermodinstein','Ripstick'))	
	let $d := my:rand(('gmail','msn','netzero'))	
	let $e := my:rand(('com','net','cc'))	
	return 
		<affiliate affiliateid="{$i}">
			<name>{$f}&nbsp;{$l}</name>
			<email>{$f}.{$l}@{$d}.{$e}</email>
			<rate>{my:between(1,25)}</rate>
			<address>
				<line>259 E. 1375 S.</line>
				<city>{ my:rand(('Kaysville','Layton','Farmington','Syracuse','Centerville')) }</city>
				<state>UT</state>
				<zip>{ my:between(84035,84039) }</zip>
			</address>
			<phone></phone>
			<affiliateStates>
				<affiliateState id="active"/>
				<affiliateState id="{ my:rand(('backaffiliate','label','shipped')) }" />
			</affiliateStates>
			<lastPaid>2011-{ my:rand(('01','02','03','04','05','06','07','08','09','10','11','12')) }-{ my:between(10,28) }</lastPaid>
		</affiliate>
}
</affiliates>;

declare function my:rand($items) { let $c := (xdmp:random() mod fn:count($items)) + 1 return $items[$c] };
declare function my:between($start,$end) { let $c := $end - $start return (xdmp:random() mod $c) + $start };

page:page (
	(
		page:meta('title', 'Order - SendMeAFilter.com'),
		page:meta('description', 'Order A Filter - SendMeAFilter.com'),
		page:meta('keywords', 'furnace heater order filter merv periodic air comfort vent ventilation'),
		page:meta('backoffice')
	),

	<div class="wrapper">
		<div class="caption">	
			<div float="right">
				<h3>Affiliates</h3>
				<div id="actions">
				<form name="theForm" method="post" onSubmit="return validateForm(this);" action="backoffice/editaffiliate">
				<table class="list-table">
					<tr>
						<td>
							<select class="facets" name="view" id="select" size="1" onChange="return Submit()">
								<option value="orderbyname" selected="selected">
									<span>by name</span>
								</option>
								<option value="needpayment">
									<span>needs payment</span>
								</option>
							</select>&nbsp;
							<input id="refreshButton" type="submit" value="Refresh"/>
						</td>
						<td align="right">
							<select class="facets" name="Action" id="select" size="1" onChange="return Submit()">
								<option value="none" selected="selected">
									<span class="all">action</span>
								</option>
								<option value="labels">
									<span class="all">export labels</span>
								</option>
								<option value="ship">
									<span class="all">pay</span>
								</option>
							</select>&nbsp;
							<input id="actButton" type="submit" value="Act"/>&nbsp;
							<input id="inputButton" type="submit" value="Add Affiliate"/>
						</td>
					</tr>
				</table>
				</form>
			</div>
			</div>
				<div id="entry">
					<table class="list-table" cellspacing="10">
					{
					let $affiliateedOrders:= for $o in $affiliates/affiliate order by $o/name return $o
					for $affiliate at $i in $affiliateedOrders
					 return (
					if($i mod 6 = 1) then
					(
						if($i = 1) then () else<tr class="bg1"><td colspan="100">&nbsp;</td></tr>,
						<tr class="tableheader">
							<td>Affiliate</td>
							<td>Info</td>
							{ for $check in $checks return <td><center>{$check}</center></td> }
						</tr>
					)
					else (),
					
					<tr class="{if($i mod 2 = 1) then 'bg1' else 'bg1-mid' }">
					<td>
						<span class="afname"><a href="/backoffice/editaffiliate?id={$affiliate/@affiliateid}">{$affiliate/name/text()}</a></span> ($0.{$affiliate/rate/text()})<br/>
						<a href="mailto:{fn:data($affiliate/email)}">{fn:data($affiliate/email)}</a><br/>
						{
							fn:string-join((
								$affiliate/address/line,
								fn:concat($affiliate/address/city,','),
								$affiliate/address/state,
								$affiliate/address/zip)," ")
						}
					</td>
					<td>
						Last Paid: { $affiliate/lastPaid/text() } - $125.45<br/>
						Total:$1334.45 (5367)<br/>
						Due: $15.45 (255)
						
					</td>
					{
					for $check in $checks
					let $checkid := fn:lower-case($check)
					return
					<td class="check">
					<center>
					{
					$space,
					element input {
					attribute type {"checkbox"},
					attribute name { fn:concat($affiliate/@affiliateid,'-',$checkid)},
					attribute value {$checkid},
					if($affiliate/affiliateStates/affiliateState[@id=fn:lower-case($check)]) then
					attribute checked { 'checked' }
					else ()
					},
					$space
					}
					</center>
					</td>
					}
					</tr>
					)}
					</table>
				</div>
			</div>
		</div>
)